package com.bckj.fastboot.upms.biz.mapper;

import com.bckj.fastboot.core.mybatisplus.mapper.FastMapper;
import com.bckj.fastboot.upms.api.dto.SysDictQuery;
import com.bckj.fastboot.upms.api.entity.SysDict;
import com.bckj.fastboot.upms.api.vo.SysDictVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.List;

@Mapper
public interface SysDictMapper extends FastMapper<SysDict> {

    @SelectProvider(method = "findAll", type = SysDictProvider.class)
    List<SysDictVO> findAll(SysDictQuery query);

    class SysDictProvider {
        public String findAll(SysDictQuery query) {
            SQL sql = new SQL();
            sql.SELECT("*");
            sql.FROM("sys_dict");
            listWhere(sql, query);
            return sql.toString();
        }

        private void listWhere(SQL sql, SysDictQuery query) {
            if (StringUtils.hasText(query.getType())) {
                sql.WHERE("type = #{query.type}");
            }
            if (StringUtils.hasText(query.getName())) {
                sql.WHERE("name like concat('%',#{query.name},'%')");
            }
            if (StringUtils.hasText(query.getCode())) {
                sql.WHERE("code = #{query.code}");
            }
        }
    }
}
